using System.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

namespace Script
{
    public class MonsterUI : MonoBehaviour
    {

        public static MonsterUI instance;

        public Image[] MonsterImages;
        
        public Transform canvasRoot; 
        
        public Button att, onHit;

        private bool OnAttAnim = false;
        private void Awake()
        {
            instance = this;
            for (int i = 0; i < MonsterImages.Length; i++)
            {
                MonsterImages[i].gameObject.SetActive(false);
            }
            att.onClick.AddListener(ATT);
            onHit.onClick.AddListener(OnHit);
        }

        private async void OnHit()
        {
            for (int i = 0; i < MonsterImages.Length; i++)
            {
                PlayHitEffect(MonsterImages[i].gameObject);
                
                await Task.Delay(200);
            }
        }

        private async void ATT()
        {
            if (OnAttAnim)
                return;
            OnAttAnim = true;
            for (int i = 0; i < MonsterImages.Length; i++)
            {
                PlayAttackEffectWithShake(MonsterImages[i].gameObject);
                
                await Task.Delay(200);
            }
            OnAttAnim = false;
        }

        public async Task StartFightUiAnim(int Mcount)
        {
            for (int i = 0; i < MonsterImages.Length; i++)
            {
                GameObject go = MonsterImages[i].gameObject;
                bool shouldShow = i < Mcount;
                go.SetActive(shouldShow);

                if (shouldShow)
                {
                    // 重置缩放和透明度
                    go.transform.localScale = Vector3.zero;

                    Image img = MonsterImages[i].GetComponent<Image>();
                    if (img != null)
                    {
                        Color c = img.color;
                        c.a = 0f;
                        img.color = c;

                        // 同时做缩放和淡入动画，持续0.5秒
                        img.DOFade(1f, 0.5f);
                        go.transform.DOScale(Vector3.one, 0.5f).SetEase(Ease.OutBack);
                    }
                    else
                    {
                        // 如果是SpriteRenderer的情况
                        var sr = MonsterImages[i].GetComponent<SpriteRenderer>();
                        if (sr != null)
                        {
                            Color c = sr.color;
                            c.a = 0f;
                            sr.color = c;

                            sr.DOFade(1f, 0.5f);
                            go.transform.DOScale(Vector3.one, 0.5f).SetEase(Ease.OutBack);
                        }
                    }
                }
            }
            
            await Task.CompletedTask;
        }
        
        public void PlayAttackEffect(GameObject monster, float attackDistance = 30f, float duration = 0.2f)
        {
            RectTransform rt = monster.GetComponent<RectTransform>();
            if (rt == null) return;

            Vector3 originalPos = rt.localPosition;
            Vector3 targetPos = originalPos + Vector3.right * attackDistance; // 假设向右攻击，你可以改方向

            rt.DOLocalMove(targetPos, duration).SetEase(Ease.OutQuad).OnComplete(() =>
            {
                rt.DOLocalMove(originalPos, duration).SetEase(Ease.InQuad);
            });
        }
        public void PlayAttackEffectWithShake(GameObject monster, float attackDistance = 30f, float duration = 0.2f, float shakeDuration = 0.3f, float shakeStrength = 10f)
        {
            RectTransform rt = monster.GetComponent<RectTransform>();
            if (rt == null || canvasRoot == null) return;

            Vector3 originalPos = rt.localPosition;
            Vector3 targetPos = originalPos + Vector3.down * attackDistance;

            Sequence seq = DOTween.Sequence();

            // 怪物向前冲击再回退
            seq.Append(rt.DOLocalMove(targetPos, duration).SetEase(Ease.OutQuad));
            seq.Append(rt.DOLocalMove(originalPos, duration).SetEase(Ease.InQuad));

            // 震屏同时进行
            seq.Join(canvasRoot.DOShakePosition(shakeDuration, shakeStrength, 20, 90, false, true));
        }
        
        public void PlayHitEffect(GameObject monster, float duration = 0.3f)
        {
            Image img = monster.GetComponent<Image>();
            if (img == null) return;

            Sequence seq = DOTween.Sequence();
            seq.Append(img.DOColor(Color.red, duration / 2));
            seq.Append(img.DOColor(Color.white, duration / 2));
        }
    }
}